package com.zyk.leetcode;

/**
 * @author zhangsan
 * @date 2021/4/4 21:39
 */
public class J36 {

    class Node {
        public int val;
        public Node left;
        public Node right;

        public Node() {
        }

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val, Node _left, Node _right) {
            val = _val;
            left = _left;
            right = _right;
        }
    }



    Node pre, cur;

    /**
     * https://leetcode-cn.com/problems/er-cha-sou-suo-shu-yu-shuang-xiang-lian-biao-lcof/
     */
    public Node treeToDoublyList(Node root) {
        if (root == null) {
            return null;
        }
        dfs(root);
        cur.left = pre;
        pre.right = cur;
        return cur;
    }

    public void dfs(Node cur) {
        if (cur == null) {
            return;
        }
        dfs(cur.left);
        if (pre != null) {
            pre.right = cur;
        } else {
            this.cur = cur;
        }
        cur.left = pre;
        pre = cur;
        dfs(cur.right);
    }


}
